package com.quectel.core.module.partyeducationtraining.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.core.partyeducationtraining.TrainingConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.partybuilding.dto.PartyOrganizationDto;
import com.quectel.core.module.partybuilding.service.PartyOrganizationService;
import com.quectel.core.module.partyeducationtraining.dao.TrainingPersonDao;
import com.quectel.core.module.partyeducationtraining.dto.TrainingPersonDto;
import com.quectel.core.module.partyeducationtraining.dto.ref.TrainingRefConcourseDto;
import com.quectel.core.module.partyeducationtraining.dto.ref.TrainingRefPersonDto;
import com.quectel.core.module.partyeducationtraining.entity.TrainingPersonEntity;
import com.quectel.core.module.partyeducationtraining.service.TrainingPersonService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2022/05/07 16:40
 */
@DubboService
public class
TrainingPersonServiceImpl extends ServiceImpl<TrainingPersonDao, TrainingPersonEntity> implements TrainingPersonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TrainingPersonServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> TRAINING_PERSON_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "TRAINING_PERSON_CACHE_BY_ID:%d", id);

    @Autowired
    private TrainingPersonDao trainingPersonDao;
    @Autowired
    private PartyOrganizationService partyOrganizationService;

    @Override
    public TrainingPersonDto selectOne(Long trainingId, Long deptPersonId) {
        TrainingPersonEntity trainingPersonEntity = trainingPersonDao.selectOne(new LambdaQueryWrapper<TrainingPersonEntity>()
                .eq(TrainingPersonEntity::getTrainingId, trainingId).eq(TrainingPersonEntity::getPartyPersonId, deptPersonId));
        return CopyUtils.copyObj(trainingPersonEntity, TrainingPersonDto.class);
    }

    @Override
    public int queryTrainingTotal(Map<String, Object> params) {
        return trainingPersonDao.queryTrainingTotal(params);
    }

    @Override
    public List<TrainingRefConcourseDto> queryTrainingList(Map<String, Object> params) {
        List<TrainingRefConcourseDto> trainingRefConcourseDtos = trainingPersonDao.queryTrainingList(params);

        final Date currentDate = new Date();
        for (TrainingRefConcourseDto dto : trainingRefConcourseDtos) {
            if (dto.getStartTime() != null && dto.getEndTime() != null) {
                if (currentDate.getTime() < dto.getStartTime().getTime()) {
                    dto.setStatus(TrainingConstants.StatusEnum.NOT_STARTED.getStatus());
                } else if (currentDate.getTime() > dto.getEndTime().getTime()) {
                    dto.setStatus(TrainingConstants.StatusEnum.ENDED.getStatus());
                } else {
                    dto.setStatus(TrainingConstants.StatusEnum.IN_PROGRESS.getStatus());
                }
            }
        }
        return trainingRefConcourseDtos;
    }

    @Override
    public void deleteByTrainingId(Long trainingId) {
        trainingPersonDao.delete(new LambdaQueryWrapper<TrainingPersonEntity>()
                .eq(TrainingPersonEntity::getTrainingId, trainingId));
    }

    @Override
    public void saveBatch(List<TrainingPersonDto> trainingPersonDtos) {
        List<TrainingPersonEntity> trainingPersonEntities = CopyUtils.copyList(trainingPersonDtos, TrainingPersonEntity.class);
        for (TrainingPersonEntity trainingPersonEntity : trainingPersonEntities) {
            trainingPersonEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(trainingPersonEntities);
    }

    @Override
    public int queryPersonTotal(Map<String, Object> params) {
        return trainingPersonDao.queryPersonTotal(params);
    }

    @Override
    public List<TrainingRefPersonDto> queryPersonList(Map<String, Object> params) {
        List<TrainingRefPersonDto> trainingPersonInfoDtos = trainingPersonDao.queryPersonList(params);
        for (TrainingRefPersonDto trainingPersonInfoDto : trainingPersonInfoDtos) {
            Long partyId = trainingPersonInfoDto.getPartyId();
            PartyOrganizationDto partyOrganizationDto = partyOrganizationService.selectCacheById(partyId);
            trainingPersonInfoDto.setParty(partyOrganizationDto);
        }
        return trainingPersonInfoDtos;
    }


    @Override
    public TrainingPersonDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                TRAINING_PERSON_CACHE_BY_ID_FUNC.apply(id),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public TrainingPersonDto selectById(Long id) {
        TrainingPersonEntity entity = trainingPersonDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, TrainingPersonDto.class));
    }

    @Override
    public List<TrainingPersonDto> queryList(Map<String, Object> params) {
        List<TrainingPersonEntity> list = trainingPersonDao.queryList(params);
        List<TrainingPersonDto> result = CopyUtils.copyList(list, TrainingPersonDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private TrainingPersonDto paddingField(TrainingPersonDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return trainingPersonDao.queryTotal(params);
    }

    @Override
    public Long save(TrainingPersonDto dto) {
        TrainingPersonEntity entity = CopyUtils.copyObj(dto, TrainingPersonEntity.class);
        entity.setId(Snowflake.nextId());
        trainingPersonDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(TrainingPersonDto dto) {
        TrainingPersonEntity entity = CopyUtils.copyObj(dto, TrainingPersonEntity.class);
        trainingPersonDao.updateById(entity);
        CacheKit.invalidRedisCache(TRAINING_PERSON_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(TrainingPersonDto dto) {
        TrainingPersonEntity entity = CopyUtils.copyObj(dto, TrainingPersonEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, trainingPersonDao);

        CacheKit.invalidRedisCache(TRAINING_PERSON_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        trainingPersonDao.deleteById(id);

        CacheKit.invalidRedisCache(TRAINING_PERSON_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
